"""
Author: Bu Xiangfeng
E-mail: xiangfeng_006@foxmail.com
Date: 2018-10-25
"""
import random
from mysql_rw import ReadDataFromSQL
mysql_data = ReadDataFromSQL()


class UserInterface:
    def __init__(self):
        self.wrong_counter = 0
        self.options = "ABCDEF"

    def start(self, limit_num=10, fail_record_only=None):
        title_index = 1
        for question_dic in mysql_data.read_data_random(limit_num=limit_num, fail_record=fail_record_only):
            print("===========\n", "%s.%s" % (title_index, question_dic['question']), sep='\n')
            index = question_dic['id']

            if question_dic['A']:
                answer_info = question_dic.get(question_dic['Answer'].strip())
                user_answer = self.option_question(question_dic)
            else:
                answer_info = question_dic['Answer'].strip()
                user_answer = self.true_false_question()

            if user_answer == answer_info:
                col_name = 'pass_count'
                print("Right!")
            else:
                col_name = 'fail_count'
                self.wrong_counter +=1
                print("WRONG!!\nRight answer:%s\n" % answer_info)

            mysql_data.update_count(col_name=col_name, index=index)
            title_index += 1

        PassRate = (limit_num - self.wrong_counter)*100/limit_num
        print("WRONG:%s  RIGHT:%s PassRate:%.2f%%" % (self.wrong_counter, (limit_num - self.wrong_counter), PassRate))

    def option_question(self, question_dic):
        questions_list = list()
        for opt in self.options:
            opt_info = question_dic.get(opt)
            if opt_info:
                questions_list.append(question_dic[opt])
            else:
                break
        random.shuffle(questions_list)

        new_order_question = dict(zip(self.options, questions_list))

        for key, value in new_order_question.items():
            print("%s.%s  " % (key, value), end="\n")

        while True:
            selected = input("\nYour answer:").upper()
            user_answer = new_order_question.get(selected)
            if user_answer:
                return user_answer
            else:
                print("Out of range, please input list 'A/B/C/D...'")

    def true_false_question(self):
        while True:
            user_answer = input("\nYour answer:").upper()
            if user_answer in "NY":
                return user_answer
            else:
                print("输入'Y' or 'N' ")


if __name__ == "__main__":
    user = UserInterface()
    questions_type = input("随机抽取题目类型(1.随机; 2.错题优先.  默认1):").strip()
    try:
        questions_type = int(questions_type)
        if questions_type == 1:
            fail_record_only = None
        else:
            fail_record_only = True
    except:
        fail_record_only = None

    questions_num = input("随机抽取题目个数（默认10个）:").strip()
    try:
        limit_num = int(questions_num)
    except:
        limit_num = 10

    user.start(limit_num, fail_record_only)
    input("..................")